using System;
using System.Collections.Generic;
using System.Text;
using System.Configuration;
using System.Data.SqlClient;
using System.Data;
using Entities;

namespace DAL
{
   public  class tbKhuvucDAL
    {
        private SqlConnection cn = null;
       public tbKhuvucDAL()
       {
           cn = new SqlConnection(ConfigurationManager.ConnectionStrings["strConnection"].ConnectionString);
           cn.Open();
          
       }
       public void Insert(tbKhuvuc  kv)
       {
           SqlCommand cm = new SqlCommand("Khu_vuc_Insert", cn);
           cm.CommandType = CommandType.StoredProcedure;
           cm.Parameters.AddWithValue("@Tenkv", kv.Tenkv  );
           cm.ExecuteNonQuery();
       }
       public void Update(tbKhuvuc  kv)
       {
           SqlCommand cm = new SqlCommand("Khu_vuc_Update", cn);
           cm.CommandType = CommandType.StoredProcedure;
           cm.Parameters.AddWithValue("@Makv", kv.Makv );
           cm.Parameters.AddWithValue("@Tenkv", kv.Tenkv );
           cm.ExecuteNonQuery();
       }
       public void Delete(int iD)
       {
           SqlCommand cm = new SqlCommand("Khu_vuc_Delete", cn);
           cm.CommandType = CommandType.StoredProcedure;
           cm.Parameters.AddWithValue("@Makv", iD );
           cm.ExecuteNonQuery();
       }
       public tbKhuvucCollection  Get(string where, int pageIndex, int pageSize, string orderBy, string orderDirection, out int totalRecords)
       {
           SqlCommand cm = new SqlCommand("Khu_vuc_Get", cn);
          cm.CommandType = CommandType.StoredProcedure;
          cm.Parameters.AddWithValue("@Where", where);
          cm.Parameters.AddWithValue("@PageIndex", pageIndex);
          cm.Parameters.AddWithValue("@PageSize", pageSize);
          cm.Parameters.AddWithValue("@OrderBy", orderBy);
          cm.Parameters.AddWithValue("@OrderDirection",orderDirection);
          cm.Parameters.Add(new SqlParameter("@TotalRecords",DbType.Int32)).Direction =ParameterDirection.Output;
          SqlDataReader dr = cm.ExecuteReader();
         tbKhuvucCollection  kvCollection = new tbKhuvucCollection  ();
         while (dr.Read())
         {
             tbKhuvuc  kv= new tbKhuvuc  ();
             kv.Makv  = int.Parse(dr.GetValue(0).ToString());
             kv.Tenkv  =  dr.GetValue(1).ToString();
             kvCollection.Add(kv);
         }
         dr.Close();
         totalRecords = int.Parse(cm.Parameters["@TotalRecords"].Value.ToString());
         return kvCollection ;
       }

   }
}
